package org.jiamandan.com.strategy;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.dto.OrderDto;
import org.jiamandan.com.strategy.enums.ChargeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Service
@Slf4j
public class IChargeContextService {

    @Autowired
    private final Map<String, IChargeBaseService> strategyMap = new ConcurrentHashMap<>();

    public IChargeContextService(Map<String, IChargeBaseService> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    public void execute(OrderDto dto) throws Exception {
        // 1-zd型号,2-yd型号,3-yx型号,4-jsy型号
        String className = ChargeEnum.getValueByKey(dto.getFactory());
        log.info("创建订单并下发充电:"+dto.toString());
        if (StringUtils.isNotBlank(className)) {
           strategyMap.get(className).execute(dto);
        }
        log.info("充电失败，请联系管理员");
    }
}
